package zz.app.ustbgsmanager.fragments;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Objects;

import zz.app.ustbgsmanager.R;
import zz.app.ustbgsmanager.adapters.ScoreListAdapter;
import zz.app.ustbgsmanager.beans.queryscore.Scores;
import zz.app.ustbgsmanager.interfaces.OnQueryScoreFailedListener;
import zz.app.ustbgsmanager.other.ShortHorizontalDividerItemDecoration;

/**
 * Created by zz on 2018/6/3 15:33.
 */
public class QueryScoreFragment extends Fragment {
    Scores scores;
    SwipeRefreshLayout swipeRefreshLayout;
    RecyclerView scoreList;
    TextView tvTotalCredits;
    TextView tvActualCredits;
    boolean hasScore = false;
    OnScoreRefreshListener onScoreRefreshListener;

    public void setOnQueryScoreFailedListener(OnQueryScoreFailedListener onQueryScoreFailedListener) {
        this.onQueryScoreFailedListener = onQueryScoreFailedListener;
    }

    OnQueryScoreFailedListener onQueryScoreFailedListener;

    public void setOnScoreRefreshListener(OnScoreRefreshListener onScoreRefreshListener) {
        this.onScoreRefreshListener = onScoreRefreshListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_queryscore, container, false);
        tvTotalCredits=view.findViewById(R.id.tvTotalCredits);
        tvActualCredits =view.findViewById(R.id.tvActualCredits);
        scoreList = view.findViewById(R.id.scoreList);
        scoreList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        scoreList.addItemDecoration(new ShortHorizontalDividerItemDecoration(getContext()));
        swipeRefreshLayout = view.findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (onScoreRefreshListener != null) {
                    onScoreRefreshListener.onScoreRefresh();
                }
            }
        });
        swipeRefreshLayout.setRefreshing(true);
        swipeRefreshLayout.setColorSchemeColors(Color.argb(255,77,165,189));
        if (hasScore) {
            showScores();
        }
        return view;
    }

    private void showScores() {
        if (scores != null) {
            scoreList.setAdapter(new ScoreListAdapter(scores.getScores(), getContext()));
            float actual=scores.getActualCredits();
            float total=scores.getTotalCredits();
            tvActualCredits.setText(String.valueOf(actual));
            tvTotalCredits.setText(String.valueOf(total));
            if(actual>=total){
                tvActualCredits.setTextColor(Color.argb(255,0,192,0));
            }

        } else {
            if(onQueryScoreFailedListener!=null){
                onQueryScoreFailedListener.onQueryScoreFailed();
                }
        }
        queryEnd();

    }

    public void setScores(Scores scores) {
        hasScore = true;
        this.scores = scores;
        showScores();
    }

    public void queryEnd() {
        swipeRefreshLayout.setRefreshing(false);
    }

    public interface OnScoreRefreshListener {
        void onScoreRefresh();
    }

}
